﻿var targetId = "table";
var sectionId="section"
$("input[type='checkbox']").iCheck({
    checkboxClass: 'icheckbox_flat-blue',
    radioClass: 'iradio_flat-blue'
});

$("#" + targetId).find("thead input[type='checkbox']").on("ifChanged", function () {
    var status = $("#table").find("thead input[type='checkbox']").parent().hasClass("checked");
    setAllItem(!status);
})

$("#" + targetId).find("tbody input[type='checkbox']").on("ifChanged", function () {
    setHeaderItem(false);
})
$("#select2").select2();
var over = "<div class='overlay'><i class='fa fa-refresh fa-spin'></i></div>";

function getAllData() {
    var data = new Array();
    var table = $("#" + targetId + " tbody input[type='checkbox']")
    for (var i = 0, l = table.length; i < l; i++) {
        if (table.eq(i).parent().hasClass("checked")) {
            var id = table.eq(i).val()
            data.push({Id:id});
        }
    }
    return data;
}

function setAllItem(flag) {
    var table = $("#table");
    var items = table.find("tbody input[type='checkbox']");
    if (flag) {
        for (var i = 0, l = items.length; i < l; i++) {
            items.eq(i).parent().addClass("checked");
        }
    } else {
        for (var i = 0, l = items.length; i < l; i++) {
            items.eq(i).parent().removeClass("checked");
        }
    }

}

function setHeaderItem(flag) {
    if (flag) {
        $("#table").find("thead input[type='checkbox']").parent().addClass("checked");
    } else {
        $("#table").find("thead input[type='checkbox']").parent().removeClass("checked");
    }
}

function setCover() {
    $("#" + sectionId ).find(".col-md-12 .box").append(over)
}
function clearCover() {
    $("#" + sectionId).find(".col-md-12 .overlay").remove()
}

function addNew() {
    window.location.href = "/PriceType/New"
}

function Delete(id) {
    if (confirm("确定要删除此价格参数！")) {
        $.ajax({
            url: "/PriceType/Remove?id=" + id,
            type: "get",
            dataType: "json",
            success: function (data) {
                window.location.reload(true);
            }, error: function (error) {
                console.log(error);
                alert("删除用户失败");
            }
        })
    }
}




function deleteAllChecked() {
    var data = getAllData()
    console.log(data)
    var count = 0
    if (data.length == 0) {
        notyWarning("请选择要删除的列表")
        return;
    }
    setCover();
    
    $.ajax({
        url: "/PriceType/RemoveChecked",
        type: "post",
        data: {guids:data},
        success: function (data) {
            count++;
            if (count == l) {
                window.location.reload(true)
            }
        }, error: function (error) {
            console.log(error)
        }
    })
}




function Edit(id) {
    window.location.href = "/PriceType/Edit?id=" + id
}   